<?php
namespace Chat\Controller;
use Common\Controller\CommonController;

/**
 * 群聊模块
 */
class ChatController extends CommonController {
	
	protected function _initialize(){
		parent::_initialize();
		$this->_mycache = S(C('REDIS_DSN'));
		$this->_auth();
		
		$this->_moduleid = 103;
		$this->_chat = new \Common\Model\ChatModel();
		$this->_chat_cust = new \Common\Model\ChatCustModel();
		$this->_face_favorite = new \Common\Model\FaceFavoriteModel();
	}
	
    
	/**
	 * 创建聊天群组
	 */
    public function createChatAction(){
    	$chatname = I('request.chatname');
    	$custids = I('request.custids');
		$this->_createChat($chatname , $custids);
    }
    
    private function _createChat($chatname , $custids , $grouptype = 1){
    	if(empty($custids)){
    		$this->status = 1000;
    		$this->msg = L('STATUS_1000');
    		$this->outPut();
    	}
    	
    	
    	$custidsArr[] = $this->_custInfo['custid'];
    	$custidsArr2 = explode(",", $custids);
    	$custidsArr = array_merge($custidsArr,$custidsArr2);
    	$custidsArr = array_unique($custidsArr);
    	$num = 0;
    	
    	if(empty($chatname)){
    		$chatname = "";
	    	foreach($custidsArr as $idx=>$custid){
	    		$custInfo = $this->_getCustInfo($custid);//用户不存在，就跳过
	    		if(empty($custInfo['nickname']))continue;
	    		
	    		if($num > 5) break;
	    		$chatname = $chatname . $custInfo['nickname'] . ",";
	    		$num++;
	    	}
	    	$chatname = substr($chatname , 0 , -1);
    	}
    	
    	$maxcustnum = C('DEFAULT_MAX_CUST_NUM');
    	$data = array();
    	$data['chatname'] = $chatname;
    	$data['grouptype'] = $grouptype;
    	$data['custid'] = $this->_custInfo['custid'];
    	$data['maxcustnum'] = $maxcustnum;
    	$data['status'] = C('DEFAULT_CHAT_STATUS');
    	$chatid = $this->_chat->addChat($data);
        if($chatid){
        	$custNum = 0;
        	$nicknames = "";
        	$data = [];
        	$custids = array();
        	$friendList = $this->_getFriendList($this->_custInfo['custid']);
        	$tmpList = array();
        	if(!empty($friendList)){
	        	foreach($friendList as $friend){
	        		$tmpList[] = $friend['custid'];
	        	}
        	}
        	
        	$idxtmp = 1;
        	$content_param[0] = array('custid'=>$this->_custInfo['custid'],'nickname'=>$this->_custInfo['nickname']);
        	
        	foreach($custidsArr as $idx=>$custid){
        		if($custNum > (int)$maxcustnum)break;//超出最大数量，就跳过
        		
        		if(!in_array($custid , $tmpList) && $custid != $this->_custInfo['custid'])continue;
        		
        		$custInfo = $this->_getCustInfo($custid);//用户不存在，就跳过
        		if(empty($custInfo))continue;
        		
        		$result = $this->_chat_cust->getCust($chatid,$custid);//如果已经是群成员，就跳过
        		if(!empty($result))continue;
        		
        		$arr = array();
        		$arr['chatid'] = $chatid;
        		$arr['custid'] = $custid;
        		$arr['status'] = 1;
        		$ret = $this->_chat_cust->addCust($arr);
        		if($ret){
        			$data[$idx]['custid'] = $custid;
        			$data[$idx]['nickname'] = $custInfo['nickname'];
        			$data[$idx]['chatid'] = $chatid;
        			
        			$custNum++;
        			if($custid != $this->_custInfo['custid'] && $custNum <= 40){//最多显示40人昵称，且不包括自己
        				//$nicknames .= empty($nicknames) ? $custInfo['nickname'] : ',' . $custInfo['nickname'];
        				
        				if($idx == 0){
        					$nicknames = $nicknames . "{".$idxtmp."}";
        				}else{
        					$nicknames = $nicknames . ",{".$idxtmp."}";
        				}
        				$content_param[$idxtmp] = array('custid'=>$custid,'nickname'=>$custInfo['nickname']);
        				$idxtmp++;
        			}
        			$custids[] = $custid;
        			$this->_delChatListCache($custid);
        		}
        	}
        	
        	if($custNum == 0){
        		$this->status = 1401;
        		$this->msg = L('STATUS_1401');
        		$this->outPut();
        	}
        	
        	$this->_chat->custNumInr($chatid , $custNum);
        	$chatInfo = $this->_getChatInfoById($chatid);
        	//$content = L('CREATE_CHAT_NOTICE',array('nickname' => $this->_custInfo['nickname'] , 'nicknames'=>$nicknames));
        	$content = "{0}把{$nicknames}加入了群聊";
        	
        	//发推送消息
        	$this->_createChatNotice($chatid , $chatInfo , $custids , $content , $content_param , $data);
        	
    		$this->data = $chatInfo;
    		$this->outPut();
    	}else{
    		$this->status = 1401;
    		$this->msg = L('STATUS_1401');
    		$this->outPut();
    	}
    }
    
    private function _createChatNotice($chatid , $chatInfo , $custids=array() , $content , $content_param , $data){
    	\im::addGroup($chatid, $custids);
    	
    	$pushInfo['name'] = $chatInfo['chatname'];
    	$pushInfo['max_count'] = $chatInfo['maxcustnum'];
    	$pushInfo['count'] = $chatInfo['custnum'];
    	$pushInfo['operationType'] = $chatInfo['chattype'];
    	$pushInfo['data'] = $data;
    	 
    	$type = 19;
    	self::sendChatNoticeMsg($type, $chatid, $content,$content_param, $pushInfo);
    }
    
    //撤销消息
    public function revokeMsgAction(){
    	$chatid = (int)I('request.chatid');
    	$custid = (int)I('request.custid');
    	$msgno = I('request.msgno');
    	$this->_revokeMsg($chatid,$custid,$msgno);
    }
    
    private function _revokeMsg($chatid,$custid,$msgno){
    	if(empty($chatid) && empty($custid) || empty($msgno)){
    		$this->status = 1000;
    		$this->msg = L('STATUS_1000');
    		$this->outPut();
    	}
    	
    	if(!empty($chatid)){
    		$chatInfo = $this->_getChatInfoById($chatid);
    		$msg['operationType'] = $chatInfo['chattype'];
    		$msg['from_cust_id'] = $this->_custInfo['custid'];
    		$msg['from_cust_name'] = $this->_custInfo['nickname'];
    		$msg['type'] = 7;
    		$msg['session_id'] = (int)$chatid;
    		$msg['session_type'] = 2;
    		$msg['msgno'] = $msgno;
    		$msg['content'] = "{0}撤回了一条消息";
    		$msg['content_param'][0] = array('custid'=>$this->_custInfo['custid'],'nickname'=>$this->_custInfo['nickname']);
    		\im::sendMsg(2, array(), array($chatid), $msg);
    	}
    	
    	if(!empty($custid)){
    		$msg['from_cust_id'] = $this->_custInfo['custid'];
    		$msg['from_cust_name'] = $this->_custInfo['nickname'];
    		$msg['type'] = 7;
    		$msg['session_id'] = (int)$this->_custInfo['custid'];
    		$msg['session_type'] = 1;
    		$msg['msgno'] = $msgno;
    		$msg['content'] = "{0}撤回了一条消息";
    		$msg['content_param'][0] = array('custid'=>$this->_custInfo['custid'],'nickname'=>$this->_custInfo['nickname']);
    		\im::sendMsg(3, array($custid), array(), $msg);
    	}
    	
    	$this->outPut();
    }
    
    
    /**
     * 获取聊天群二维码
     */
    public function getChatQRCodeAction(){
    	$chatid = (int)I('request.chatid');
    	$this->_getChatQRCode($chatid);
    }
    
    private function _getChatQRCode($chatid){
        if(empty($chatid)){
    		$this->status = 1000;
    		$this->msg = L('STATUS_1000');
    		$this->outPut();
    	}
    	
    	$chatInfo = $this->_getChatInfoById($chatid);
    	$data['chatid'] = $chatInfo['chatid'];
    	$data['chatname'] = $chatInfo['chatname'];
    	$data['status'] = $chatInfo['status'];
    	$data['controller'] = CONTROLLER_NAME;
    	//$data = CONTROLLER_NAME . "://" . json_encode($data);
    	$data = C('QR_CODE_INFO_URL') . "?data=" . \aes::encrypt(json_encode($data) , C('TOKEN_SALT'));
    	 
    	$qrcodefile = "/tmp/chat_{$chatid}.png";
    	$logofile = "";//准备好的群avatar图片地址或图片url
    	 
    	\tools::makeQrcode($data , $qrcodefile , $logofile);
    }
    
    
    /**
     * 查找聊天群组列表
     */
    public function searchChatAction(){
    	$searchKey = I('request.searchKey');
    	$pageIndex = (int)I('request.pageIndex');
    	$pageSize = (int)I('request.pageSize');
    	$this->_searchChat($searchKey , $pageIndex , $pageSize);
    }
    
    private function _searchChat($searchKey , $pageIndex = 0 , $pageSize = 0){
    	if(empty($searchKey)){
    		$this->status = 1000;
    		$this->msg = L('STATUS_1000');
    		$this->outPut();
    	}
    	
    	$resultData = array();
    	$data = array();
    	if($pageIndex)$pageIndex--;
    	$data['pageIndex'] = $pageIndex;
    	$data['pageSize'] = $pageSize;
    	$data['searchKey'] = $searchKey;
    	$chatList = $this->_chat->getChatList($data);
    	if(!empty($chatList['data'])){
    		$inChat = array();
    		$resultData = $this->_getChatListCache($this->_custInfo['custid']);
    		if(!empty($resultData)){
    			foreach($resultData as &$chat){
    				$inChat[$chat['chatid']] = 1;
    			}
    		}
    		
	    	foreach($chatList['data'] as &$chat){
	    		unset($chat['custid']);
	    		unset($chat['introduce']);
	    		unset($chat['createtime']);
	    		unset($chat['updatetime']);
	    		$chat['_isjoin'] = empty($inChat[$chat['chatid']]) ? "0" : "1";
	    	}
	    	
	    	$resultData = $chatList['data'];
    	}
    	 
    	$this->data = $resultData;
    	$this->outPut();
    }
    
    
    /**
     * 获取聊天群组列表
     */
    public function getChatListAction(){
    	$this->_getMyChatList();
    }
    
    
    private function _getMyChatList(){
    	$resultData = $this->_getChatList($this->_custInfo['custid']);
    	$this->data = $resultData;
    	$this->outPut();
    }
    
    
    /**
     * 增加聊天群组人员
     */
    public function addChatCustsAction(){
    	$chatid = (int)I('request.chatid');
    	$custids = I('request.custids');
    	$this->_addChatCusts($chatid,$custids);
    }
    
    
    
    private function _addChatCusts($chatid,$custids){
    	if(empty($chatid) || empty($custids)){
    		$this->status = 1000;
    		$this->msg = L('STATUS_1000');
    		$this->outPut();
    	}
    	
    	$chatInfo = $this->_checkCurrentCust($chatid);
    	
    	$custidsArr = explode(",", $custids);
    	$custidsArr = array_unique($custidsArr);
    	if(!empty($custidsArr)){
        	$custNum = 0;
        	$nicknames = "";
        	$data = [];
        	$custids = array();
        	$friendList = $this->_getFriendList($this->_custInfo['custid']);
        	$tmpList = array();
        	if(!empty($friendList)){
        		foreach($friendList as $friend){
        			$tmpList[] = $friend['custid'];
        		}
        	}
        	
        	$idxtmp = 1;
        	$content_param[0] = array('custid'=>$this->_custInfo['custid'],'nickname'=>$this->_custInfo['nickname']);
        	
    		foreach($custidsArr as $idx=>$custid){
    			if($custNum > (int)$chatInfo['maxcustnum']-(int)$chatInfo['custnum'])break;//超出最大数量，就跳过
    			
    			if(!in_array($custid , $tmpList) && $custid != $this->_custInfo['custid'])continue;
    			
    			$custInfo = $this->_getCustInfo($custid);//用户不存在，就跳过
    			if(empty($custInfo))continue;
    			
    			$result = $this->_chat_cust->getCust($chatid,$custid);//如果已经是群成员，就跳过
    			if(!empty($result))continue;
    			
    			$arr = array();
    			$arr['chatid'] = $chatid;
    			$arr['custid'] = $custid;
    			$arr['status'] = 1;
    			$ret = $this->_chat_cust->addCust($arr);
    			if($ret){
    				$data[$idx]['custid'] = $custid;
    				$data[$idx]['nickname'] = $custInfo['nickname'];
    				$data[$idx]['chatid'] = $chatid;
    				
    				$this->_delChatCustCache($chatid);
    				$this->_delChatListCache($custid);
    				
    				$custNum++;
    				if($custid != $this->_custInfo['custid'] && $custNum <= 40){//最多显示40人昵称，且不包括自己
    					//$nicknames .= empty($nicknames) ? $custInfo['nickname'] : ',' . $custInfo['nickname'];
    					
    					if($idx == 0){
    						$nicknames = $nicknames . "{".$idxtmp."}";
    					}else{
    						$nicknames = $nicknames . ",{".$idxtmp."}";
    					}
    					$content_param[$idxtmp] = array('custid'=>$custid,'nickname'=>$custInfo['nickname']);
    					$idxtmp++;
    				}
    				$custids[] = $custid;
    				
    				$this->_setAvatarNotice($chatid);
    			}
    		}
    		
    		if($custNum == 0){
    			$this->status = 1402;
    			$this->msg = L('STATUS_1402');
    			$this->outPut();
    		}
    		 
    		$this->_chat->custNumInr($chatid , $custNum);
    		$chatInfo = $this->_getChatInfoById($chatid);
    		//$content = L('ADD_CHAT_CUST_NOTICE',array('nickname' => $this->_custInfo['nickname'] , 'nicknames'=>$nicknames));
    		$content = "{0}邀请{$nicknames}加入了群聊";
    		
    		//发推送消息
    		$this->_addCustsNotice($chatid , $chatInfo , $custids , $content , $content_param , $data);
    		
    		$this->outPut();
    	}else{
    		$this->status = 1402;
    		$this->msg = L('STATUS_1402');
    		$this->outPut();
    	}
    }
    
    protected function _addCustsNotice($chatid , $chatInfo , $custids=array() , $content , $content_param , $data){
    	\im::addGroupCusts($chatid, $custids);
    	
    	$pushInfo['name'] = $chatInfo['chatname'];
    	$pushInfo['max_count'] = $chatInfo['maxcustnum'];
    	$pushInfo['count'] = $chatInfo['custnum'];
    	$pushInfo['operationType'] = $chatInfo['chattype'];
    	$pushInfo['data'] = $data;
    	 
    	$type = 19;
    	self::sendChatNoticeMsg($type, $chatid, $content,$content_param, $pushInfo);
    }
    
    
    
    /**
     * 申请加入聊天群组
     */
    public function applyJoinAction(){
    	$chatid = (int)I('request.chatid');
    	$this->_applyJoin($chatid);
    }
    
    
    private function _applyJoin($chatid){
    	if(empty($chatid)){
    		$this->status = 1000;
    		$this->msg = L('STATUS_1000');
    		$this->outPut();
    	}
    	
    	$chatInfo = $this->_getChatInfoById($chatid);
    	
    	$result = $this->_chat_cust->getCust($chatid,$this->_custInfo['custid']);
    	if(!empty($result)){
    		$this->status = 1408;
    		$this->msg = L('STATUS_1408');
    		$this->outPut();
    	}
    	
    	
    	$applyKey = \cachekey::$key['BASE_KEY_CONF']['APPLY_CHAT_TIME'].$this->_custInfo['custid'].":".$chatid;
    	$timelast = $this->_mycache->get($applyKey);
    	 
    	if(__ENV__ == "production"){
    		if(time() - (int)$timelast > (int)C('DENY_APPLY_CHAT_EXPIRE')){
    			$this->_mycache->set($applyKey , time());
    			$this->_mycache->expire($applyKey , C('DENY_APPLY_CHAT_EXPIRE'));
    			 
    			//发推送消息
    			//$content = L('APPLY_JOIN_CHAT_NOTICE',array('nickname' => $this->_custInfo['nickname']));
    			$content = "{0}申请加入群聊";
    			$content_param[0] = array('custid'=>$this->_custInfo['custid'],'nickname'=>$this->_custInfo['nickname']);
    			
    			$this->_applyJoinNotice($chatid , $chatInfo , $this->_custInfo , $content , $content_param);
    		}
    	}else{
    		
    		//发推送消息
    		//$content = L('APPLY_JOIN_CHAT_NOTICE',array('nickname' => $this->_custInfo['nickname']));
    		$content = "{0}申请加入群聊";
    		$content_param[0] = array('custid'=>$this->_custInfo['custid'],'nickname'=>$this->_custInfo['nickname']);
    		
    		$this->_applyJoinNotice($chatid , $chatInfo , $this->_custInfo , $content , $content_param);
    	}
    	
    	$this->outPut();
    }
    
    
    private function _applyJoinNotice($chatid , $chatInfo , $custInfo , $content , $content_param){
    	$noticeData = array();
    	$noticeData['content'] = $content;
    	$noticeData['custid'] = $custInfo['custid'];
    	$noticeData['tcustid'] = $chatInfo['custid'];
    	$noticeData['moduleid'] = $this->_moduleid;
    	$noticeData['businessid'] = $chatid;
    	$noticeid = $this->_createNotice($noticeData);
    	
    	$pushInfo['from_cust_name'] = $custInfo['nickname'];
    	$pushInfo['from_cust_id'] = (int)$custInfo['custid'];
    	$pushInfo['notice_id'] = (int)$noticeid;
    	$pushInfo['operationType'] = $chatInfo['chattype'];
    	
    	$type = 25;
    	self::sendChatNoticeMsg($type, $chatid, $content,$content_param, $pushInfo);
    }
    
    
    /**
     * 接受某人申请加入聊天群组
     */
    public function acceptJoinAction(){
    	$noticeid = (int)I('request.noticeid');
    	$accept = (int)I('request.accept');
    	$this->_acceptJoin($noticeid , $accept);
    }
    
    
    private function _acceptJoin($noticeid , $accept = 1){
    	if(empty($noticeid)){
    		$this->status = 1000;
    		$this->msg = L('STATUS_1000');
    		$this->outPut();
    	}
    	
    	//check notice
    	$noticeInfo = $this->_getNotice(array('noticeid'=>$noticeid));
    	if(!empty($noticeInfo)){
    		if($this->_custInfo['custid'] != $noticeInfo['tcustid']){
    			$this->status = 1001;
    			$this->msg = L('STATUS_1001');
    			$this->outPut();
    		}
    		$custid = $noticeInfo['custid'];
    		$chatid = $noticeInfo['businessid'];
    	}else{
    		$this->status = 1001;
    		$this->msg = L('STATUS_1001');
    		$this->outPut();
    	}
    	
    	if(empty($accept)){//拒绝
    		$this->outPut();
    	}
        
        $custInfo = $this->_getCustInfo($custid);
        $data = array();
        $data['chatid'] = $chatid;
        $data['custid'] = $custid;
        //$data['nickname'] = $custInfo['nickname'];
        $data['status'] = 1;
        $ret = $this->_chat_cust->addCust($data);
    	if($ret){
    		$this->_chat->custNumInr($chatid , 1);
    		$this->_delChatCustCache($chatid);
    		$this->_delChatListCache($custid);
    		
    		//$content = L('ACCEPT_JOIN_CHAT_NOTICE',array('nickname' => $custInfo['nickname']));
    		$content = "{0}加入群聊";
    		$content_param[0] = array('custid'=>$this->_custInfo['custid'],'nickname'=>$this->_custInfo['nickname']);
    		
    		$chatInfo = $this->_chat->getChatInfoById($chatid);
    		//发推送消息
    		$this->_acceptJoinNotice($chatid , $chatInfo , $custInfo , $content , $content_param);
    		
    		$this->_setAvatarNotice($chatid);
    		
    		$this->outPut();
    	}else{
    		$this->status = 1409;
    		$this->msg = L('STATUS_1409');
    		$this->outPut();
    	}
    }
    
    
    private function _acceptJoinNotice($chatid , $chatInfo , $custInfo , $content , $content_param){
    	\im::addGroupCusts($chatid, array($custInfo['custid']));
    	$data[0]['cust_id'] = $custInfo['custid'];
    	$data[0]['cust_name'] = $custInfo['nickname'];
    	
    	$pushInfo['name'] = $chatInfo['chatname'];
	    $pushInfo['max_count'] = $chatInfo['maxcustnum'];
	    $pushInfo['count'] = $chatInfo['custnum'];
	    $pushInfo['operationType'] = $chatInfo['chattype'];
	    $pushInfo['data'] = $data;
    	
    	$type = 19;
    	self::sendChatNoticeMsg($type, $chatid, $content,$content_param, $pushInfo);
    }
    
    
    /**
     * 判断当前用户是否加入过群组
     */
    public function checkCustExistAction(){
    	$chatid = (int)I('request.chatid');
    	$this->_checkCustExist($chatid);
    }
    
    private function _checkCustExist($chatid){
    	if(empty($chatid)){
    		$this->status = 1000;
    		$this->msg = L('STATUS_1000');
    		$this->outPut();
    	}
		$result = $this->_chat_cust->getCust($chatid,$this->_custInfo['custid']);
		if(!empty($result)){
			$this->status = 1412;
			$this->msg = L('STATUS_1412');
			$this->outPut();
		}else{
			$this->outPut();
		}
    }
    
    
    /**
     * 自动加入群
     */
    public function autoJoinAction(){
    	$chatid = (int)I('request.chatid');
    	$sign = I('request.sign');
    	$time = I('request.time');
    	$regtype = (int)I('request.regtype');
    	$this->_autoJoin($chatid , $time , $sign , $regtype);
    }
    
    private function _autoJoin($chatid , $time , $sign , $regtype=1){
    	$regtype = empty($regtype) ? 1 : $regtype;
    	if(empty($chatid) || empty($time) || empty($sign) || !in_array($regtype , array(1,2,3))){
    		$this->status = 1000;
    		$this->msg = L('STATUS_1000');
    		$this->outPut();
    	}
    	if($regtype == 1)
    		$username = $this->_custInfo['mobile'];
    	else 
    		$username = $this->_custInfo['email'];
    	
    	$this->_checkSign($username,$time,$sign);
        
        $data = array();
        $data['chatid'] = $chatid;
        $data['custid'] = $this->_custInfo['custid'];
        //$data['nickname'] = $this->_custInfo['nickname'];
        $data['status'] = 1;
        $ret = $this->_chat_cust->addCust($data);
    	if($ret){
    		$this->_chat->custNumInr($chatid , 1);
    		$this->_delChatCustCache($chatid);
    		$this->_delChatListCache($this->_custInfo['custid']);
    		
    		//$content = L('ACCEPT_JOIN_CHAT_NOTICE',array('nickname' => $this->_custInfo['nickname']));
    		$content = "{0}加入群聊";
    		$content_param[0] = array('custid'=>$this->_custInfo['custid'],'nickname'=>$this->_custInfo['nickname']);
    		
    		$chatInfo = $this->_chat->getChatInfoById($chatid);
    		//发推送消息
    		$this->_acceptJoinNotice($chatid , $chatInfo , $this->_custInfo , $content , $content_param);
    		
    		$this->_setAvatarNotice($chatid);
    		
    		$this->outPut();
    	}else{
    		$this->status = 1410;
    		$this->msg = L('STATUS_1410');
    		$this->outPut();
    	}
    	
    }
    
    
    /**
     * 获取聊天群组成员信息
     */
    public function getChatCustsAction(){
    	$chatid = (int)I('request.chatid');
    	$this->_getChatCusts($chatid);
    }
    
    
    private function _getChatCusts($chatid){
    	if(empty($chatid)){
    		$this->status = 1000;
    		$this->msg = L('STATUS_1000');
    		$this->outPut();
    	}
    	
    	$this->_checkCurrentCust($chatid);
    	
    	$custList = $this->_getChatCust($chatid);
    	if(!empty($custList)){
    		$this->data = $custList;
    		$this->outPut();
    	}else{
    		$this->status = 1413;
    		$this->msg = L('STATUS_1413');
    		$this->outPut();
    	}
    }
    
    
    /**
     * 设置聊天群组名称
     */
    public function setChatNameAction(){
    	$chatid = (int)I('request.chatid');
    	$chatname = I('request.chatname');
    	$this->_setChatName($chatid , $chatname);
    }
    
    
    private function _setChatName($chatid , $chatname=""){
    	if(empty($chatid)){
    		$this->status = 1000;
    		$this->msg = L('STATUS_1000');
    		$this->outPut();
    	}
    	
    	$this->_checkCurrentCust($chatid , 1);
    	$data = array();
    	$data['chatid'] = $chatid;
    	$data['chatname'] = $chatname;
    	$ret = $this->_chat->updateChat($data);
    	if($ret){
    		$custList = $this->_chat_cust->getCustIdsByChatId($chatid);
    		foreach($custList as &$cust){
    			$this->_delChatListCache($cust['custid']);
    		}
    		
    		$chatInfo = $this->_getChatInfoById($chatid);
    		//$content = L('SET_CHAT_NAME_NOTICE',array('nickname' => $this->_custInfo['nickname'] , 'chatname'=>$chatInfo['chatname']));
    		$content = "{0}修改群名称为“{$chatname}”";
    		$content_param[0] = array('custid'=>$this->_custInfo['custid'],'nickname'=>$this->_custInfo['nickname']);
    		
    		//发推送消息
    		$this->_setChatNameNotice($chatid , $chatInfo , $content , $content_param);
    		$this->outPut();
    	}else{
    		$this->status = 1415;
    		$this->msg = L('STATUS_1415');
    		$this->outPut();
    	}
    }
    
    private function _setChatNameNotice($chatid , $chatInfo , $content , $content_param){
    	$pushInfo['name'] = $chatInfo['chatname'];
    	$pushInfo['max_count'] = $chatInfo['maxcustnum'];
    	$pushInfo['count'] = $chatInfo['custnum'];
    	$pushInfo['operationType'] = $chatInfo['chattype'];
    	
    	$type = 21;
    	self::sendChatNoticeMsg($type, $chatid, $content , $content_param, $pushInfo);
    }
    
    private function _setChatIntroduceNotice($chatid , $chatInfo , $content , $content_param){
    	$pushInfo['introduce'] = $chatInfo['introduce'];
    	$pushInfo['max_count'] = $chatInfo['maxcustnum'];
    	$pushInfo['count'] = $chatInfo['custnum'];
    	$pushInfo['operationType'] = $chatInfo['chattype'];
    	 
    	$type = 22;
    	
    	$msg['from_cust_name'] = $this->_custInfo['nickname'];
    	$msg['from_cust_id'] = (int)$this->_custInfo['custid'];
    	$msg['type'] = $type;
    	$msg['session_id'] = (int)$chatid;
    	$msg['session_type'] = 2;
    	$msg['content'] = $content;
    	$msg['content_param'] = $content_param;
    	$msg = array_merge($msg, $pushInfo);
    	\im::sendMsg(2, array(), array($chatid), $msg);
    }
    
    
    /**
     * 设置聊天群组介绍
     */
    public function setChatIntroduceAction(){
    	$chatid = (int)I('request.chatid');
    	$introduce = I('request.introduce');
    	$this->_setChatIntroduce($chatid , $introduce);
    }
    
    
    private function _setChatIntroduce($chatid , $introduce=""){
    	if(empty($chatid)){
    		$this->status = 1000;
    		$this->msg = L('STATUS_1000');
    		$this->outPut();
    	}
    	
    	$this->_checkCurrentCust($chatid , 1);
    	$data = array();
    	$data['chatid'] = $chatid;
    	$data['introduce'] = $introduce;
    	$ret = $this->_chat->updateChat($data);
    	if($ret){
    		$chatInfo = $this->_getChatInfoById($chatid);
    		//$content = L('SET_CHAT_INTRODUCE_NOTICE',array('nickname' => $this->_custInfo['nickname'] , 'introduce'=>$chatInfo['introduce']));
    		$content = $introduce;//"{0}修改群介绍为“{$introduce}”";
    		$content_param[0] = array('custid'=>$this->_custInfo['custid'],'nickname'=>$this->_custInfo['nickname']);
    		
    		$this->_setChatIntroduceNotice($chatid , $chatInfo , $content , $content_param);
    		$this->outPut();
    	}else{
    		$this->status = 1416;
    		$this->msg = L('STATUS_1416');
    		$this->outPut();
    	}
    }
    
    
    /**
     * 设置开放邀请
     */
    public function setChatOpenAction(){
    	$chatid = (int)I('request.chatid');
    	$isopen = (int)I('request.isopen');
    	$this->_setChatOpen($chatid , $isopen);
    }
    
    
    private function _setChatOpen($chatid , $isopen){
    	if(empty($chatid)){
    		$this->status = 1000;
    		$this->msg = L('STATUS_1000');
    		$this->outPut();
    	}
    
    	$this->_checkCurrentCust($chatid , 1);
    	$data = array();
    	$data['chatid'] = $chatid;
    	$data['isopen'] = $isopen;
    	$ret = $this->_chat->updateChat($data);
    	if($ret){
    		$this->outPut();
    	}else{
    		$this->status = 1416;
    		$this->msg = L('STATUS_1416');
    		$this->outPut();
    	}
    } 
    
    
    /**
     * 删除聊天群组
     */
    public function deleteChatAction(){
    	$chatid = (int)I('request.chatid');
    	$this->_deleteChat($chatid , 1);
    }
    
    
    protected function _deleteChat($chatid , $checkadmin = 1){
    	if(empty($chatid)){
    		$this->status = 1000;
    		$this->msg = L('STATUS_1000');
    		$this->outPut();
    	}
    	
    	$chatInfo = $this->_checkCurrentCust($chatid , $checkadmin);
    		
    	$custList = $this->_chat_cust->getCustIdsByChatId($chatid);
    	
		$data = array();
		$data['chatid'] = $chatid;
		$ret = $this->_chat->deleteChat($data);
    	if($ret){
    		$this->_delChatCustCache($chatid);
    		if(!empty($custList)){
    			foreach($custList as &$cust){
    				$this->_delChatListCache($cust['custid']);
    			}
    			//$this->_chat_cust->deleteCust(array('chat'=>$chatid));利用外键自动删除
    		}
    		
    		//$content = L('DEL_CHAT_NOTICE',array('nickname' => $this->_custInfo['nickname']));
    		$content = "{0}解散了该群";
    		$content_param[0] = array('custid'=>$this->_custInfo['custid'],'nickname'=>$this->_custInfo['nickname']);
    		
    		//发推送消息
    		$this->_deleteChatNotice($chatid , $chatInfo , $content , $content_param);
    		$this->outPut();
    	}else{
    		$this->status = 1417;
    		$this->msg = L('STATUS_1417');
    		$this->outPut();
    	}
    }
    
    
    private function _deleteChatNotice($chatid , $chatInfo , $content , $content_param){
    	$pushInfo['name'] = $chatInfo['chatname'];
    	$pushInfo['max_count'] = $chatInfo['maxcustnum'];
    	$pushInfo['count'] = $chatInfo['custnum'];
    	$pushInfo['operationType'] = $chatInfo['chattype'];
    	
    	$type = 27;
    	self::sendChatNoticeMsg($type, $chatid, $content, $content_param , $pushInfo);
    	//暂停1秒
    	sleep(1);
    	\im::delGroup($chatid);
    }
    
    
    /**
     * 退出聊天群组
     */
    public function exitChatAction(){
    	$chatid = (int)I('request.chatid');
    	$this->_exitChat($chatid);
    }
    
    private function _exitChat($chatid){
    	if(empty($chatid)){
    		$this->status = 1000;
    		$this->msg = L('STATUS_1000');
    		$this->outPut();
    	}
    	
    	$chatInfo = $this->_checkCurrentCust($chatid);
    	if($chatInfo['custid'] == $this->_custInfo['custid']){
			$this->status = 1440;
    		$this->msg = L('STATUS_1440');
    		$this->outPut();
    	}
    	
    	if($chatInfo['custnum'] <= 2){
    		$this->status = 1418;
    		$this->msg = L('STATUS_1418');
    		$this->outPut();
    	}
    	
    	$data['chatid'] = $chatid;
    	$data['custid'] = $this->_custInfo['custid'];
    	$deleteNum = $this->_chat_cust->deleteCust($data);
    	if((int)$deleteNum > 0){
    		$this->_chat->custNumDecr($chatid , 1);
    		$this->_delChatCustCache($chatid);
    		$this->_delChatListCache($this->_custInfo['custid']);
    		
    		//$content = L('EXIT_CHAT_NOTICE',array('nickname' => $this->_custInfo['nickname']));
    		$content = "{0}已退出该群";
    		$content_param[0] = array('custid'=>$this->_custInfo['custid'],'nickname'=>$this->_custInfo['nickname']);
    		
    		//发推送消息
    		$this->_exitChatNotice($chatid , $chatInfo , $this->_custInfo , $content , $content_param);
    		$this->outPut();
    	}else{
    		$this->status = 1419;
    		$this->msg = L('STATUS_1419');
    		$this->outPut();
    	}
    }
    
    
    private function _exitChatNotice($chatid , $chatInfo , $custInfo , $content , $content_param){
    	\im::delGroupCusts($chatid, array($custInfo['custid']));
    	$data[0]['custid'] = $custInfo['custid'];
    	$data[0]['nickname'] = $custInfo['nickname'];
    	
    	$pushInfo['name'] = $chatInfo['chatname'];
    	$pushInfo['max_count'] = $chatInfo['maxcustnum'];
    	$pushInfo['count'] = $chatInfo['custnum'];
    	$pushInfo['operationType'] = $chatInfo['chattype'];
    	$pushInfo['data'] = $data;
    	
    	$type = 20;
    	self::sendChatNoticeMsg($type, $chatid, $content,$content_param, $pushInfo);
    }
    
    /**
     * 删除聊天群组人员
     */
    public function deleteChatCustsAction(){
    	$chatid = (int)I('request.chatid');
    	$custids = I('request.custids');
    	$this->_deleteChatCusts($chatid , $custids);
    }
    
    
    private function _deleteChatCusts($chatid , $custids){
    	if(empty($chatid) || empty($custids)){
    		$this->status = 1000;
    		$this->msg = L('STATUS_1000');
    		$this->outPut();
    	}
    	
    	$chatInfo = $this->_checkCurrentCust($chatid , 1);
    	
    	$custids = explode(",", $custids);
    	$custids = array_unique($custids);
    	
    	if($chatInfo['custnum']-count($custids) <= 2){
    		$this->status = 1418;
    		$this->msg = L('STATUS_1418');
    		$this->outPut();
    	}
    	
    	$data = array();
    	$data['chatid'] = $chatid;
    	$data['custids'] = $custids;
    	$deleteNum = $this->_chat_cust->deleteCust($data);
    	if((int)$deleteNum > 0){
    		$this->_chat->custNumDecr($chatid , $deleteNum);
    		$this->_delChatCustCache($chatid);
    		
    		$idxtmp = 1;
    		$content_param[0] = array('custid'=>$this->_custInfo['custid'],'nickname'=>$this->_custInfo['nickname']);
    		
    		$data = [];
    		$nicknames = "";
    		foreach($custids as $idx=>$custid){
    			$custInfo = $this->_getCustInfo($custid);
    			$this->_delChatListCache($custid);
    			//$nicknames .= empty($nicknames) ? $custInfo['nickname'] : ',' . $custInfo['nickname'];
    			
    			if($idx == 0){
    				$nicknames = $nicknames . "{".$idxtmp."}";
    			}else{
    				$nicknames = $nicknames . ",{".$idxtmp."}";
    			}
    			$content_param[$idxtmp] = array('custid'=>$custid,'nickname'=>$custInfo['nickname']);
    			$idxtmp++;
    			
    			$data[$idx]['custid'] = $custid;
    			$data[$idx]['nickname'] = $custInfo['nickname'];
    			$data[$idx]['chatid'] = $chatid;
    		}
    		
    		//$content = L('DEL_CHAT_CUST_NOTICE',array('nickname' => $this->_custInfo['nickname'] , 'nicknames'=>$nicknames));
    		$content = "{0}把{$nicknames}移出该群";
    		
    		$chatInfo = $this->_getChatInfoById($chatid);
    		//发推送消息
    		$this->_deleteChatCustsNotice($chatid , $chatInfo , $custids , $content , $content_param , $data);

    		$this->outPut();
    	}else{
    		$this->status = 1421;
    		$this->msg = L('STATUS_1421');
    		$this->outPut();
    	}
    }
    
    
    private function _deleteChatCustsNotice($chatid , $chatInfo , $custids=array() , $content , $content_param , $data){
    	$pushInfo['name'] = $chatInfo['chatname'];
    	$pushInfo['max_count'] = $chatInfo['maxcustnum'];
    	$pushInfo['count'] = $chatInfo['custnum'];
    	$pushInfo['operationType'] = $chatInfo['chattype'];
    	$pushInfo['data'] = $data;
    	 
    	$type = 20;
    	self::sendChatNoticeMsg($type, $chatid, $content,$content_param, $pushInfo);
    	\im::delGroupCusts($chatid, $custids);
    }
    
    
    
    /**
     * 转移群主
     */
    public function setChatCreatorAction(){
    	$chatid = (int)I('request.chatid');
    	$custid = (int)I('request.custid');
    	$this->_setChatCreator($chatid , $custid);
    }
    
    
    private function _setChatCreator($chatid , $custid){
    	if(empty($chatid) || empty($custid)){
    		$this->status = 1000;
    		$this->msg = L('STATUS_1000');
    		$this->outPut();
    	}
    	
    	$this->_checkCurrentCust($chatid , 1);
    	
    	$result = $this->_chat_cust->getCust($chatid,$custid);
    	if(empty($result)){
    		$this->status = 1433;
    		$this->msg = L('STATUS_1433');
    		$this->outPut();
    	}
    	
    	$data = array();
    	$data['chatid'] = $chatid;
    	$data['custid'] = $custid;
    	$ret = $this->_chat->updateChat($data);
    	if($ret){
    		$custInfo = $this->_getCustInfo($custid);
    		$msg['from_cust_name'] = $this->_custInfo['nickname'];
    		$msg['from_cust_id'] = (int)$this->_custInfo['custid'];
    		$msg['type'] = 24;
    		$msg['session_id'] = (int)$chatid;
    		$msg['session_type'] = 2;
    		$msg['content'] = "{0}已成为新群主";
    		$msg['content_param'][0] = array('custid'=>$custid,'nickname'=>$custInfo['nickname']);
    		\im::sendMsg(2, array(), array($chatid), $msg);
    		
    		$this->outPut();
    	}else{
    		$this->status = 1431;
    		$this->msg = L('STATUS_1431');
    		$this->outPut();
    	}
    }
    
    
    /**
     * 设置在群里面显示的昵称
     */
    public function setNicknameAction(){
    	$chatid = (int)I('request.chatid');
    	$nickname = I('request.nickname');
    	$this->_setNickname($chatid , $nickname);
    }
    
    
    private function _setNickname($chatid , $nickname){
    	if(empty($chatid) || empty($nickname)){
    		$this->status = 1000;
    		$this->msg = L('STATUS_1000');
    		$this->outPut();
    	}
    	
    	$this->_checkCurrentCust($chatid);
    	 
    	$data = array();
    	$data['chatid'] = $chatid;
    	$data['custid'] = $this->_custInfo['custid'];
    	$data['nickname'] = $nickname;
    	$ret = $this->_chat_cust->updateCust($data);
    	if($ret){
    		$this->_delChatCustCache($chatid);
    		$this->outPut();
    	}else{
    		$this->status = 1435;
    		$this->msg = L('STATUS_1435');
    		$this->outPut();
    	}
    }
    
    
    /**
     * 修改聊天群组头像
     */
    public function setAvatarAction(){
    	$chatid = (int)I('request.chatid');
    	$this->_setAvatar($chatid);
    }
    
    private function _setAvatar($chatid){
    	if(empty($chatid)){
    		$this->status = 1000;
    		$this->msg = L('STATUS_1000');
    		$this->outPut();
    	}
    	
    	$this->_checkCurrentCust($chatid , 1);
    	 
    	$filedata = $_FILES['uploadedFile']['name'];
    	$errorNo = $_FILES['uploadedFile']['error'];
    	if($errorNo){
    		$this->_getUploadError($errorNo);
    	}
    	 
    	if(!empty($filedata)){
    		$mimetype = strtolower($_FILES['uploadedFile']['type']);
    		$res = \tools::makeAvatarFilePath(C('CHAT_AVATAR_PATH') , $chatid , $mimetype);
    		$filetarget = $res['filetarget'];
    		$targetpath = $res['targetpath'];
    		$width = $res['width'];
    		$height = $res['height'];
    
    		\tools::delAllFile($targetpath);
    		 
    		$ret = move_uploaded_file($_FILES['uploadedFile']['tmp_name'], $filetarget);
    		if($ret){
    			$data['chatid'] = $chatid;
    			$data['avatar'] = $filetarget;
    			$this->_chat->updateChat($data);
    			
    			$this->_setAvatarNotice($chatid , 0);
    			
    			$this->data = array('width'=>$width,'height'=>$height);
    			$this->outPut();
    		}else{
    			$this->status = 1429;
    			$this->msg = L('STATUS_1429');
    			$this->outPut();
    		}
    	}
    }
    
    
    
    protected function _setAvatarNotice($chatid , $checkFile = 1){
    	$custList = $this->_getChatCust($chatid);
    	
    	if($checkFile){
    		$res = \tools::makeAvatarFilePath(C('CHAT_AVATAR_PATH') , $chatid);
    	    $targetpath = $res['targetpath'];
	    	$avatars[] =  "{$targetpath}{$chatid}.jpg";
	    	$avatars[] =  "{$targetpath}{$chatid}.png";
	    	$avatars[] =  "{$targetpath}{$chatid}.gif";
	    	$avatar = "";
	    	foreach($avatars as $avatar){
	    		if(file_exists($avatar)){
	    			return true;
	    		}
	    	}
    	}
    	 
    	if(!empty($custList) && count($custList)<=9){
    		foreach($custList as $cust){
    			$custs[] = $cust['custid'];
    		}
    	
    		//系统消息
    		$msg['chat_id'] = $chatid;
    		$msg['type'] = 26;
    		$msg['content'] = "";
    		\im::sendMsg(3, $custs, array(), $msg);
    	}
    }
    
    
    /**
     * 获取聊天群组的详细信息
     */
    public function getChatDetailAction(){
    	$chatid = (int)I('request.chatid');
    	$this->_getChatDetail($chatid);
    }
    
    private function _getChatDetail($chatid){
    	if(empty($chatid)){
    		$this->status = 1000;
    		$this->msg = L('STATUS_1000');
    		$this->outPut();
    	}
    
    	$chatInfo = $this->_getChatInfoById($chatid);
    	if($chatInfo['custid'] == $this->_custInfo['custid']){//判断当前用户是否是群主
    		$chatInfo['_isadmin'] = "1";
    		$chatInfo['nickname'] = $this->_custInfo['nickname'];
    	}else{
    		$chatInfo['_isadmin'] = "0";
    		$custInfo = $this->_getCustInfo($chatInfo['custid']);
    		$chatInfo['nickname'] = $custInfo['nickname'];
    	}
    
    	$chatInfo['_exist'] = "0";
    	
    	$custList = $this->_getChatCust($chatid);
    	
    	if(!empty($custList)){
    		foreach($custList as &$v){
    			$custid = $v['custid'];
    			if($custid == $this->_custInfo['custid']){
    				$chatInfo['_exist'] = "1";
    				$chatInfo['_nickname'] = $v['nickname'];
    				if(!empty($v['msgsetting'])){
    					$chatInfo['_msgsetting'] = $v['msgsetting'];//json_decode($v['msgsetting'] , true);
    				}
    			}
    			
    			unset($v['msgsetting']);
    		}
    		
    		$custList = array_slice($custList, 0, 38);//只显示39个
    	}
    
    	$chatInfo['custList'] = $custList;
    	$this->data = $chatInfo;
    	$this->outPut();
    }
    
    
    /**
     * 举报群
     */
    public function chatReportAction(){
    	$chatid = (int)I('request.chatid');
    	$content = I('request.content');
    	$spamtype = (int)I('request.spamtype');
    	$this->_chatReport($chatid , $content , $spamtype);
    }
    
    private function _chatReport($chatid , $content , $spamtype){
    	if(empty($chatid) || empty($content)){
    		$this->status = 1000;
    		$this->msg = L('STATUS_1000');
    		$this->outPut();
    	}
    	 
    	$data = array();
    	$data['custid'] = $this->_custInfo['custid'];
    	$data['chatid'] = $chatid;
    	$data['content'] = $content;
    	$data['spamtype'] = $spamtype;
    	
    	if(empty($this->_chat_report)){
    		$this->_chat_report = new \Chat\Model\ChatReportModel();
    	}
    	 
    	$result = $this->_chat_report->getSpam($data);
    	if(!empty($result)){
    		$this->status = 1404;
    		$this->msg = L('STATUS_1404');
    		$this->outPut();
    	}
    	 
    	$ret = $this->_chat_report->addSpam($data);
    	if($ret){
    		$this->outPut();
    	}else{
    		$this->status = 1403;
    		$this->msg = L('STATUS_1403');
    		$this->outPut();
    	}
    }
    
    
    /**
     * 发送文件
     */
    public function sendFileAction(){
    	$this->_sendFile();
    }
    
    
    
    /**
     * 读取所有离线消息
     */
    public function getUnreadMsgListAction(){
    	$pageSize = (int)I('request.pageSize');
    	$this->_getUnreadMsgList($pageSize);
    }
    
    private function _getUnreadMsgList($pageSize){
    	$redisArr = C('CHAT_REDIS_DSN');
    	$custRedisIdx = \tools::talkCacheHash($this->_custInfo['custid'] , count($redisArr));
    	$this->_talkcache[$custRedisIdx] = S($redisArr[$custRedisIdx]); 
    	if(empty($this->_talkcache[$custRedisIdx])) $this->outPut();
    	
    	
    	/* $cache_token = $this->_talkcache[$custRedisIdx]->get(\cachekey::$key['TALK_KEY_CONF']['CHAT_AUTH_CUST'] . $this->_custInfo['custid']);
    	if($this->_token != $cache_token){
    		$this->outPut();
    	} */
    	
    	$page_size = !empty($pageSize) ? $pageSize : 100;
    	
    	$chatIds = array();
    	$chatList = $this->_getChatListCache($this->_custInfo['custid']);
    	if(empty($chatList)){
    		$chatList = $this->_chat_cust->getChatIdsByCustId($this->_custInfo['custid']);
    	}
    	if(!empty($chatList)){
    		foreach($chatList as &$chat){
    			array_push($chatIds , (int)$chat['chatid']);
    		}
    	}
    	$joinChat = $chatIds;
    	
    	$cust_unread_key = \cachekey::$key['TALK_KEY_CONF']['MSG_UNREAD'] . $this->_custInfo['custid'];
    	
    	$unread_msg = array();
    	$total = 10000;
    	while($total > 0) {
    		$total--;
    		$unread_msg_list = $this->_talkcache[$custRedisIdx]->lRange($cust_unread_key, 0, $page_size-1);
    		if(empty($unread_msg_list)) {
    			break;
    		}
    			
    		foreach ($unread_msg_list as $msg_id) {
    			$msg = "";
    			for($i=0;$i<3;$i++){
    				//根据hash值决定从哪个实例取消息
    				if($msg_id[0] == "-"){
						$tmp_msg_id = bcadd("18446744073709551616" , $msg_id);
    				}else{
    					$tmp_msg_id = $msg_id;
    				}
    				$msgRedisIdx = \tools::talkCacheHash($tmp_msg_id , count($redisArr));
    				if(empty($this->_talkcache[$msgRedisIdx])){
    					$this->_talkcache[$msgRedisIdx] = S($redisArr[$msgRedisIdx]);
    				}
    				
    				$msg = $this->_talkcache[$msgRedisIdx]->get($msg_id);
    				if(!empty($msg))break;
    			}
    	
    			if (empty($msg)) {
    				$this->_talkcache[$custRedisIdx]->lREM($cust_unread_key, $msg_id, 0);
    				continue;
    			}else{
    				$tmp_msg = $msg;//json_decode($msg , true);ThinkPHP的redis cache类默认做了json_decode
    				if(empty($tmp_msg) || !is_array($tmp_msg)){
    					$this->_talkcache[$custRedisIdx]->lREM($cust_unread_key, $msg_id, 0);
    					continue;
    				}
    					
    				if((int)$tmp_msg['chat_id'] > 0){
    					if(!in_array($tmp_msg['chat_id'] , $joinChat)){
    						$this->_talkcache[$custRedisIdx]->lREM($cust_unread_key, $msg_id, 0);
    						continue;
    					}
    				}
    	
    				$msg_id = (string)$msg_id;
    				$unread_msg[$msg_id] = $tmp_msg;
    			}
    				
    			if(count($unread_msg) >= $page_size) {
    				break;
    			}
    		}
    			
    		if(count($unread_msg_list) < $page_size){
    			break;
    		}
    	
    		if(count($unread_msg) >= $page_size) {
    			break;
    		}
    	}
    	
    	if(!empty($unread_msg)){
    		foreach($unread_msg as $msg_id=>$msgRow){
    			$tmpdata[] = $msgRow;
    		}
    		$returnData = $tmpdata;
    	}else{
    		$returnData = array();
    	}
    	
    	$this->data = $returnData;
    	$this->outPut();
    }
    
    
    /**
     * 设置读取离线消息后的回执
     */
    public function setUnreadMsgListACKAction(){
    	$msgids = I('request.msgids');
    	$this->_setUnreadMsgListACK($msgids);
    }
    
    
    private function _setUnreadMsgListACK($msgids){
    	$redisArr = C('CHAT_REDIS_DSN');
    	$custRedisIdx = \tools::talkCacheHash($this->_custInfo['custid'] , count($redisArr));
    	$this->_talkcache[$custRedisIdx] = S($redisArr[$custRedisIdx]);
    	if(empty($this->_talkcache[$custRedisIdx])) $this->outPut();
    	
    	if(empty($msgids)){
    		$this->status = 1000;
    		$this->msg = L('STATUS_1000');
    		$this->outPut();
    	}
    	
		$msg_ids = explode(',', $msgids);
		if(count($msg_ids) > 0){
			$cust_unread_key = \cachekey::$key['TALK_KEY_CONF']['MSG_UNREAD'] . $this->_custInfo['custid'];
			foreach ($msg_ids as $msg_id) {
				$msg_id = trim($msg_id);
				if ($msg_id) {
					$this->_talkcache[$custRedisIdx]->lREM($cust_unread_key, $msg_id, 0);
				}
			}
		}
		
		$this->outPut();
    }
    
    
    /**
     * 注册设备号
     */
    public function regDeviceNoAction(){
    	$deviceno = I('request.deviceno');
    	$this->_regDeviceNo($deviceno);
    }
    
    
    private function _regDeviceNo($deviceno){
    	if(empty($this->_mycache)) $this->outPut();
    	
    	if (!empty($deviceno)) {
    		$this->_mycache->hSET(\cachekey::$key['TALK_KEY_CONF']['CUST_DEVICE'], $this->_custInfo['custid'], $deviceno);
    		$this->_mycache->hSET(\cachekey::$key['TALK_KEY_CONF']['DEVICE_CUST'], $deviceno, $this->_custInfo['custid']);
    	}else{
    		$deviceno_orgin = $this->_mycache->hget(\cachekey::$key['TALK_KEY_CONF']['CUST_DEVICE'], $this->_custInfo['custid']);
    		if (!empty($deviceno_orgin)) {
    			$this->_mycache->hDEL(\cachekey::$key['TALK_KEY_CONF']['DEVICE_CUST'], $deviceno_orgin);
    		}
    			
    		$this->_mycache->hDEL(\cachekey::$key['TALK_KEY_CONF']['CUST_DEVICE'], $this->_custInfo['custid']);
    	}
    	
    	$this->data = array('device_no'=>$deviceno);
    	$this->outPut();
    }
    
    
    /**
     * 注销设备号
     */
    public function unregDeviceNoAction(){
    	$deviceno = I('request.deviceno');
    	$this->_unregDeviceNo($deviceno);
    }
    
    
    private function _unregDeviceNo($deviceno){
    	if(empty($this->_mycache)) $this->outPut();
    	
		if (!empty($deviceno)) {
			$this->_mycache->hDEL(\cachekey::$key['TALK_KEY_CONF']['DEVICE_CUST'], $deviceno);
		}else{
			$deviceno_orgin = $this->_mycache->hget(\cachekey::$key['TALK_KEY_CONF']['CUST_DEVICE'], $this->_custInfo['custid']);
			if(!empty($deviceno_orgin)){
				$this->_mycache->hDEL(\cachekey::$key['TALK_KEY_CONF']['DEVICE_CUST'], $deviceno_orgin);
			}
		}
		
		$this->_mycache->hDEL(\cachekey::$key['TALK_KEY_CONF']['CUST_DEVICE'], $this->_custInfo['custid']);
		
		$deviceno = empty($deviceno)?$deviceno_orgin:$deviceno;
		$deviceno = empty($deviceno)?"":$deviceno;
    	
    	$this->data = array('device_no'=>$deviceno);
		$this->outPut();
    }
    
    
    
    /**
     * 设置未读消息数
     */
    public function setUnreadCountAction(){
    	$unreadcount = (int)I('request.unreadcount');
    	$this->_setUnreadCount($unreadcount);
    }
    
    
    private function _setUnreadCount($unreadcount){
    	if(empty($this->_mycache)) $this->outPut();
    	
    	$unreadcount = $unreadcount < 0 ? 0 : $unreadcount;
    	$this->_mycache->hSET(\cachekey::$key['TALK_KEY_CONF']['PUSH_NOTICE_COUNT'], $this->_custInfo['custid'], $unreadcount);
    	
    	$this->outPut();
    }
    
    protected function _checkCurrentCust($chatid , $isadmin = 0){
    	if(empty($chatid)){
    		$this->status = 1000;
    		$this->msg = L('STATUS_1000');
    		$this->outPut();
    	}
    	
    	$chatInfo = $this->_chat->getChatInfoById($chatid);
    	if(empty($chatInfo)){
    		$this->status = 1400;
    		$this->msg = L('STATUS_1400');
    		$this->outPut();
    	}else{
    		$result = $this->_chat_cust->getCust($chatid,$this->_custInfo['custid']);
    		if(empty($result)){
    			$this->status = 1405;
    			$this->msg = L('STATUS_1405');
    			$this->outPut();
    		}else{
    			if($isadmin && $chatInfo['custid'] != $this->_custInfo['custid']){
    				$this->status = 1406;
    				$this->msg = L('STATUS_1406');
    				$this->outPut();
    			}
    		}
    		
    		return $chatInfo;
    	}
    }
    
    
    
    protected function _getChatInfoById($chatid){
    	$chatInfo = $this->_chat->getChatInfoById($chatid);
    	if(empty($chatInfo)){
    		$this->status = 1400;
    		$this->msg = L('STATUS_1400');
    		$this->outPut();
    	}else{
    		return $chatInfo;
    	}
    }
    
    
    private static function sendChatNoticeMsg($type, $chatid, $content , $content_param=[], $pushInfo = array()) {
    	$msg['type'] = $type;
    	$msg['session_id'] = (int)$chatid;
    	$msg['session_type'] = 2;
    	$msg['content'] = $content;
    	$msg['content_param'] = $content_param;
    	$msg = array_merge($msg, $pushInfo);
    	\im::sendMsg(2, array(), array($chatid), $msg);
    }
    


    /**
     * 消息设置
     */
    public function msgSettingAction(){
    	$chatid = (int)I('request.chatid');
    	$top = (int)I('request.top');
    	$push = (int)I('request.push');
    	$shownick = (int)I('request.shownick');
    	$this->_msgSetting($chatid , $top , $push , $shownick);
    }
    
    
    private function _msgSetting($chatid , $top , $push , $shownick){
    	if(empty($chatid)){
    		$this->status = 1000;
    		$this->msg = L('STATUS_1000');
    		$this->outPut();
    	}
    	
    	$data = array();
    	$data['chatid'] = $chatid;
    	$data['custid'] = $this->_custInfo['custid'];
    	$setting = array('top'=>(string)$top , 'push'=>(string)$push , 'shownick'=>(string)$shownick);
    	$data['msgsetting'] = json_encode($setting);
    	$ret = $this->_chat_cust->updateCust($data);
    	if($ret){
    		if($push <= 0)
    			$this->_mycache->srem(\cachekey::$key['BASE_KEY_CONF']['NO_PUSH_CHAT'].$chatid, $this->_custInfo['custid']);
    		else
    			$this->_mycache->sadd(\cachekey::$key['BASE_KEY_CONF']['NO_PUSH_CHAT'].$chatid, $this->_custInfo['custid']);
    		
    		$this->_delChatCustCache($chatid);
    		
    		$this->outPut();
    	}else{
    		$this->status = 1436;
    		$this->msg = L('STATUS_1436');
    		$this->outPut();
    	}
    }
    
    
    
    /**
     * 表情收藏
     */
    public function faceAddAction(){
    	$img = I('request.img');
    	$this->_faceAdd($img);
    }
    
    private function _faceAdd($img){
    	$imgArr = json_decode($img , true);
    	if(!is_array($imgArr) || empty($imgArr)){
    		$this->status = 1000;
    		$this->msg = L('STATUS_1000');
    		$this->outPut();
    	}
    
    	$data['img'] = $img;
    	$data['custid'] = $this->_custInfo['custid'];
    	$favoriteid = $this->_face_favorite->addFavorite($data);
    	
    	$this->data['favoriteid'] = $favoriteid;
    	$this->outPut();
    }
    
    
    
    /**
     * 删除表情收藏
     */
    public function faceDeleteAction(){
    	$favoriteids = I('request.favoriteids');
    	$this->_faceDelete($favoriteids);
    }
    
    private function _faceDelete($favoriteids){
    	if(empty($favoriteids)){
    		$this->status = 1000;
    		$this->msg = L('STATUS_1000');
    		$this->outPut();
    	}
    	
    	$favoriteids = explode(",", $favoriteids);
    
    	$data['favoriteids'] = $favoriteids;
    	$data['custid'] = $this->_custInfo['custid'];
    	$this->_face_favorite->deleteFavorite($data);
    	$this->outPut();
    }
    
    
    
    /**
     * 我的表情收藏
     */
    public function myFaceListAction(){
    	$pageIndex = (int)I('request.pageIndex');
    	$pageSize = (int)I('request.pageSize');
    	$this->_myFaceList($pageIndex,$pageSize);
    }
    
    private function _myFaceList($pageIndex = 0 , $pageSize = 0){
    	$data = array();
    	if($pageIndex)$pageIndex--;
    	$data['pageIndex'] = $pageIndex;
    	$data['pageSize'] = $pageSize;
    	$data['custid'] = $this->_custInfo['custid'];
    	$faceList = $this->_face_favorite->getFavoriteList($data);
    	if(!empty($faceList['data'])){
    		foreach($faceList['data'] as &$face){
    			$face['img'] = json_decode($face['img']);
    		}
    	}
    	$this->data = $faceList;
    	$this->outPut();
    }
}
